/********************************
 *
 
 
 * ThaoP Create on 20090720
 
 
 *
 * ******************************/
using System;
using System.Collections;
using EO.DB;
using System.Data;
using System.Data.SqlClient;

namespace EO
{
/// <summary>
/// monhoc
/// </summary>
public class monhoc
{

	#region ***** fields & properties ***** 

	private System.String _Ma_mon;
	public System.String Ma_mon
	{ 
		get 
		{ 
			return _Ma_mon;
		}
		set 
        { 
			_Ma_mon = value;
		}
	}

	private System.String _Ma_nganh;
	public System.String Ma_nganh
	{ 
		get 
		{ 
			return _Ma_nganh;
		}
		set 
        { 
			_Ma_nganh = value;
		}
	}

	private System.String _Ten_mon;
	public System.String Ten_mon
	{ 
		get 
		{ 
			return _Ten_mon;
		}
		set 
        { 
			_Ten_mon = value;
		}
	}

	private System.String _Ghi_chu;
	public System.String Ghi_chu
	{ 
		get 
		{ 
			return _Ghi_chu;
		}
		set 
        { 
			_Ghi_chu = value;
		}
	}

	#endregion

	/// <summary>
	/// monhoc
	/// </summary>
	public monhoc()
	{
	}


	/// <summary>
	/// monhoc
	/// </summary>
	public monhoc(System.String Ma_mon,System.String Ma_nganh,System.String Ten_mon,System.String Ghi_chu)
	{
		this.Ma_mon = Ma_mon;
		this.Ma_nganh = Ma_nganh;
		this.Ten_mon = Ten_mon;
		this.Ghi_chu = Ghi_chu;
	}

	#region *****persistance managing methods


    /// <summary>
    /// Thuc thi thu tuc sp_monhoc_DeleteAll
    /// </summary>
    public void monhoc_DeleteAll()
    {
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_DeleteAll";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.ExecuteNonQuery();
            myBase.CloseConnection(myConn);
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_DeleteAll : \n" + myErr.ToString() + reqSQL));
        }
    }

    /// <summary>
    /// Thuc thi thu tuc sp_monhoc_DeleteBy_Ma_nganh
    /// </summary>
    public void monhoc_DeleteBy_Ma_nganh(int khoachinh)
    {
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_DeleteBy_Ma_nganh";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);

            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_nganh", System.Data.SqlDbType.Char, 3));
            myCommand.Parameters["@Ma_nganh"].Value = Ma_nganh;

            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.ExecuteNonQuery();


            myBase.CloseConnection(myConn);
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_DeleteBy_Ma_nganh : \n" + myErr.ToString() + reqSQL));
        }
    }

    /// <summary>
    /// Thuc thi thu tuc sp_monhoc_DeleteByPrimaryKey
    /// </summary>
    public void monhoc_DeleteByPrimaryKey(string Ma_mon)
    {
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_DeleteByPrimaryKey";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);

            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_mon", System.Data.SqlDbType.Char, 7));
            myCommand.Parameters["@Ma_mon"].Value = Ma_mon;

            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.ExecuteNonQuery();


            myBase.CloseConnection(myConn);
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_DeleteByPrimaryKey : \n" + myErr.ToString() + reqSQL));
        }
    }

    /// <summary>
    /// Thuc thi thu tuc sp_monhoc_GetAlllay ve toan bo bang
    /// </summary>
    public DataSet monhoc_GetAll()
    {
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_GetAll";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter myDataAdapter = new SqlDataAdapter(myCommand);
            System.Data.DataSet myResult = new System.Data.DataSet();
            myDataAdapter.Fill(myResult, "monhoc");
            myBase.CloseConnection(myConn);
            return myResult;
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_GetAll : \n" + myErr.ToString() + reqSQL));
        }
    }

    /// <summary>
    /// Thuc thi thu tuc sp_monhoc_GetBy_Ma_nganh
    /// </summary>
    public DataSet monhoc_GetBy_Ma_nganh(string Ma_nganh)
    {       
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_GetBy_Ma_nganh";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);

            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_nganh", System.Data.SqlDbType.Char, 3));
            myCommand.Parameters["@Ma_nganh"].Value = Ma_nganh;
            myCommand.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter myDataAdapter = new SqlDataAdapter(myCommand);
            System.Data.DataSet myResult = new System.Data.DataSet();
            myDataAdapter.Fill(myResult, "monhoc");
            myBase.CloseConnection(myConn);
            return myResult;


            myBase.CloseConnection(myConn);
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_GetBy_Ma_nganh : \n" + myErr.ToString() + reqSQL));
        }
    }


    /// <summary>
    /// Thuc thi thu tuc sp_monhoc_GetByPrimaryKey
    /// </summary>
    public monhoc monhoc_GetByPrimaryKey(int khoachinh)
    {
        monhoc mymonhoc = new monhoc();
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_GetByPrimaryKey";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);

            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_mon", System.Data.SqlDbType.Char, 7));
            myCommand.Parameters["@Ma_mon"].Value = mymonhoc.Ma_mon;

            myCommand.CommandType = CommandType.StoredProcedure;
            SqlDataReader myReader = myCommand.ExecuteReader();
            if (myReader.Read())
            {
                mymonhoc.Ma_mon = myReader[0].ToString();
                try { mymonhoc.Ma_nganh = myReader[1].ToString(); }
                catch { mymonhoc.Ma_nganh = ""; }
                try { mymonhoc.Ten_mon = myReader[2].ToString(); }
                catch { mymonhoc.Ten_mon = ""; }
                try { mymonhoc.Ghi_chu = myReader[3].ToString(); }
                catch { mymonhoc.Ghi_chu = ""; }
            }
            myBase.CloseConnection(myConn);
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_GetByPrimaryKey : \n" + myErr.ToString() + reqSQL));
        }
        return mymonhoc;
    }

    /// <summary>
    /// Thuc thi thu tuc sp_monhoc_Insert
    /// </summary>
    public void monhoc_Insert(monhoc mymonhoc)
    {
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_Insert";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);

            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_mon", System.Data.SqlDbType.Char, 7));
            myCommand.Parameters["@Ma_mon"].Value = mymonhoc.Ma_mon;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_nganh", System.Data.SqlDbType.Char, 3));
            myCommand.Parameters["@Ma_nganh"].Value = mymonhoc.Ma_nganh;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ten_mon", System.Data.SqlDbType.NVarChar, 300));
            myCommand.Parameters["@Ten_mon"].Value = mymonhoc.Ten_mon;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ghi_chu", System.Data.SqlDbType.NVarChar, 400));
            myCommand.Parameters["@Ghi_chu"].Value = mymonhoc.Ghi_chu;

            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.ExecuteNonQuery();


            myBase.CloseConnection(myConn);
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_Insert : \n" + myErr.ToString() + reqSQL));
        }
    }

    /// <summary>
    /// Thuc thi thu tuc sp_monhoc_Update
    /// </summary>
    public void monhoc_Update(monhoc mymonhoc)
    {
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_Update";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);

            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_nganh", System.Data.SqlDbType.Char, 3));
            myCommand.Parameters["@Ma_nganh"].Value = mymonhoc.Ma_nganh;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ten_mon", System.Data.SqlDbType.NVarChar, 300));
            myCommand.Parameters["@Ten_mon"].Value = mymonhoc.Ten_mon;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ghi_chu", System.Data.SqlDbType.NVarChar, 400));
            myCommand.Parameters["@Ghi_chu"].Value = mymonhoc.Ghi_chu;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_mon", System.Data.SqlDbType.Char, 7));
            myCommand.Parameters["@Ma_mon"].Value = mymonhoc.Ma_mon;

            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.ExecuteNonQuery();


            myBase.CloseConnection(myConn);
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_Update : \n" + myErr.ToString() + reqSQL));
        }
    }
    public void monhoc_Update(monhoc mymonhoc,string strMa_mon_old)
    {
        CConnect myBase = new CConnect();
        string reqSQL = "sp_monhoc_Update";
        try
        {
            SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
            SqlCommand myCommand = new SqlCommand(reqSQL, myConn);

            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_nganh", System.Data.SqlDbType.Char, 3));
            myCommand.Parameters["@Ma_nganh"].Value = mymonhoc.Ma_nganh;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ten_mon", System.Data.SqlDbType.NVarChar, 300));
            myCommand.Parameters["@Ten_mon"].Value = mymonhoc.Ten_mon;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ghi_chu", System.Data.SqlDbType.NVarChar, 400));
            myCommand.Parameters["@Ghi_chu"].Value = mymonhoc.Ghi_chu;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_mon", System.Data.SqlDbType.Char, 7));
            myCommand.Parameters["@Ma_mon"].Value = mymonhoc.Ma_mon;
            myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Ma_mon_old", System.Data.SqlDbType.Char, 7));
            myCommand.Parameters["@Ma_mon_old"].Value = strMa_mon_old;
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.ExecuteNonQuery();


            myBase.CloseConnection(myConn);
        }
        catch (Exception myErr)
        {
            throw (new Exception("Loi thao tac csdl - sp_monhoc_Update : \n" + myErr.ToString() + reqSQL));
        }
    }

	/// <summary>
	/// get an instance of object
	/// </summary>
	/// <param name="myID"></param>
	public monhoc getObjectmonhoc(System.String myID)
	{
		monhoc mymonhoc = new monhoc();
		CConnect myBase = new CConnect();
		string reqSQL = "SELECT Ma_mon,Ma_nganh,Ten_mon,Ghi_chu FROM monhoc WHERE (Ma_mon='"+myID+"')";
		try
		{
				SqlConnection myConn = myBase.OpenConnection(myBase.cnxString);
				SqlCommand myCommand = new SqlCommand(reqSQL,myConn);
			SqlDataReader myReader = myCommand.ExecuteReader();
			if(myReader.Read())
			{
				mymonhoc.Ma_mon = myReader[0].ToString();
				try{mymonhoc.Ma_nganh = myReader[1].ToString();}catch{mymonhoc.Ma_nganh = "";}
				try{mymonhoc.Ten_mon = myReader[2].ToString();}catch{mymonhoc.Ten_mon = "";}
				try{mymonhoc.Ghi_chu = myReader[3].ToString();}catch{mymonhoc.Ghi_chu = "";}
			}
		myBase.CloseConnection(myConn);
		}
	catch(Exception myErr)
	{
		throw(new Exception(myErr.ToString() + reqSQL));
	}
	return mymonhoc;
	}

	/// <summary>
	/// get a DataSet from records
	/// </summary>
	public DataSet Getmonhoc()
	{
		CConnect myBase = new CConnect();
		string reqSQL = "SELECT * FROM monhoc";
			try
			{
				CConnect myCConnect = new CConnect();	
				DataSet myResult = myCConnect.GetDataSet(reqSQL);
				return myResult;
			}
			catch(Exception myErr)
			{
				throw(new Exception(myErr.ToString() + reqSQL));
			}
	}

	#endregion


}
 
 
 }
